package com.fy56.platform.logistics.restful.api.controller.gmms;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.InterFaceResult;
import com.fy56.platform.logistics.service.wx.IGmmsCommonService;
import com.fy56.platform.logistics.utils.MD5Util;
import com.fy56.platform.logistics.utils.StringUtil;
import com.wordnik.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;


/*
 * @项目名称：
 * @类名称：GmmsCommonController
 * @类描述：共用接口controller
 * @创建人：
 * @作者单位：哈尔滨飞扬软件技术有限公司
 * --------------------------------------
 * @创建时间：2019-04-16 16:54:15
 * @version：1.0.0
 */
@Api(value = "GmmsCommonController", description = "仓配发货小程序-通用Controller", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/gmmsCommonController")
public class GmmsCommonController {

    // 日志工具类
    private static Logger logger = Logger.getLogger(GmmsCommonController.class);
    private SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private IGmmsCommonService iGmmsCommonServlet;

    /**
     * 仓配小程序 公共转发访问接口
     *
     * http://192.168.3.252:8082/logistics-restful-api/gmmsCommonController/getAccessCommonRequest
     * data = {"code":"123456","url":"http://192.168.3.220:2222/gmms_web/","companyId":""}
     */
    @RequestMapping(value = "/getAccessCommonRequest", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseEntity getAccessCommonRequest(@RequestParam String url, @RequestParam String param){
        InterFaceResult result = null;
        Long beginTime = System.currentTimeMillis();    // 开始时间
        try{
            String paramsErrorMsg = "";
            if (StringUtil.isEmpty(url)) {
                paramsErrorMsg = "url参数不可为空 ";
            }
            if (StringUtil.isEmpty(param)) {
                paramsErrorMsg += "param参数不可为空 ";
            }
            if (StringUtil.isEmpty(paramsErrorMsg)) {
                result = iGmmsCommonServlet.getAccessCommonRequestService(url, param);
            } else {
                result = new InterFaceResult(false, 101,paramsErrorMsg, null);
            }
        }catch(Exception ex){
            result = new InterFaceResult(false, 101, "通用接口功能报错-getAccessCommonRequest url："+ url +",  param:" + param + ",  error: " + ex.getMessage(),null);
            logger.error("通用接口功能报错-getAccessCommonRequest：",ex);
        }finally {
            Long endTime = System.currentTimeMillis();
            logger.info("MethodName: getAccessCommonRequest方法");
            logger.info("Url: " + url );
            logger.info("Param: " + param);
            logger.info("result: " + ((result==null)?"": JSON.toJSONString(result)));
            logger.info("BeginTime: " + dataFormat.format(new Date(beginTime)) + "   EndTime: " + dataFormat.format(new Date(endTime)));
            logger.info("ExecuteTime: " + dataFormat.format(new Date(endTime - beginTime)));
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


    /**
     * 获取注册验证码
     *
     * http://192.168.3.252:8082/logistics-restful-api/gmmsCommonController/getValidateCode
     * url = http://192.168.3.220:2222/gmms_web/ifs/message_sendRegSms_resultAction.action
     * companyId = M123456
     * phone = 15000000000
     * loginName = 15000000000
     * key = 0123456789
     */
    @RequestMapping(value = "/getValidateCode", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseEntity getValidateCode(@RequestParam String url, @RequestParam String companyId, @RequestParam String loginName, @RequestParam String phone, @RequestParam String key){
        InterFaceResult result = null;
        Long beginTime = System.currentTimeMillis();    // 开始时间
        try{
            String paramsErrorMsg = "";
            if (StringUtil.isEmpty(url)) {
                paramsErrorMsg = "url参数不可为空 ";
            }
            if (StringUtil.isEmpty(companyId)) {
                paramsErrorMsg += "companyId参数不可为空 ";
            }
            if (StringUtil.isEmpty(loginName)) {
                paramsErrorMsg += "loginName参数不可为空 ";
            }
            if (StringUtil.isEmpty(phone)) {
                paramsErrorMsg += "phone参数不可为空 ";
            }
            if (StringUtil.isEmpty(key)) {
                paramsErrorMsg += "key参数不可为空 ";
            }
            if (StringUtil.isEmpty(paramsErrorMsg)) {
                result = iGmmsCommonServlet.getValidateCodeService(url, companyId, loginName, phone, key);
            } else {
                result = new InterFaceResult(false, 101,paramsErrorMsg, null);
            }
        }catch(Exception ex){
            result = new InterFaceResult(false, 101, "通用接口功能报错-getValidateCode url："+ url +",  companyId:" + companyId +",  loginName:" + loginName
                    +",  phone:" + phone +",  key:" + key + ",  error: " + ex.getMessage(),null);
            logger.error("通用接口功能报错-getValidateCode：",ex);
        }finally {
            Long endTime = System.currentTimeMillis();
            logger.info("MethodName: getValidateCode方法");
            logger.info("Url: " + url );
            logger.info("Param: companyId:" + companyId +",  loginName:" + loginName +",  phone:" + phone +",  key:" + key );
            logger.info("result: " + ((result==null)?"": JSON.toJSONString(result)));
            logger.info("BeginTime: " + dataFormat.format(new Date(beginTime)) + "   EndTime: " + dataFormat.format(new Date(endTime)));
            logger.info("ExecuteTime: " + dataFormat.format(new Date(endTime - beginTime)));
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


    /**
     * 仓配立即发货接口
     * http://192.168.3.252:8082/logistics-restful-api/gmmsCommonController/sendClientAddOrder
     */
    @RequestMapping(value = "/sendClientAddOrder",method = {RequestMethod.POST, RequestMethod.GET })
    public ResponseEntity sendClientAddOrder(@RequestParam String url, String param,String key){
        InterFaceResult result = null;
        Long beginTime = System.currentTimeMillis();   // 开始时间
        String sign ="";
        try{
            String paramsErrorMsg = "";
            sign = MD5Util.MD5Encoder(param+key,"utf-8");
            if (StringUtil.isEmpty(sign)) {
                paramsErrorMsg = "sign参数不可为空 ";
            }
            if (StringUtil.isEmpty(url)) {
                paramsErrorMsg = "url参数不可为空 ";
            }
            // 获取立即发货信息
            if (StringUtil.isEmpty(paramsErrorMsg)){
                result = iGmmsCommonServlet.addClientAddOrderService(url,param, sign);
            } else {
                result = new InterFaceResult(false, 101,paramsErrorMsg, null);
            }
        }catch(Exception ex){
            result = new InterFaceResult(false, 101, "通用接口功能报错-sendClientAddOrder url："+ url +",  param:" + param +",  sign:" + sign + ",  error: " + ex.getMessage(),null);
            logger.error("通用接口功能报错-sendClientAddOrder：",ex);
        }finally {
            Long endTime = System.currentTimeMillis();
            logger.info("MethodName: getValidateCode方法");
            logger.info("Url: " + url );
            logger.info("Param: param:" + param +",  sign:" + sign);
            logger.info("result: " + ((result==null)?"": JSON.toJSONString(result)));
            logger.info("BeginTime: " + dataFormat.format(new Date(beginTime)) + "   EndTime: " + dataFormat.format(new Date(endTime)));
            logger.info("ExecuteTime: " + dataFormat.format(new Date(endTime - beginTime)));
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


}
